﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using DataManager;
using System.Drawing.Printing;

using UploadManager;
//using StoreClientModule;
using UnityCore;

namespace MBB_Store_Sales_Report
{
    public partial class frmCheckList : Form
    {
        SynchronizeData sync = new SynchronizeData();
        Threader t = new Threader();
        readDetails path = new readDetails();

        public frmCheckList()
        {
            InitializeComponent();

            btnPrint.Click +=new EventHandler(btnPrint_Click);
            btnView.Click += new EventHandler(btnView_Click);
            btnClose.Click += new EventHandler(btnClose_Click);
            panel2.EnabledChanged += new EventHandler(panel2_EnabledChanged);

            sync.ProcessCompleted += new UnityCore.CoreHandler(sync_ProcessCompleted);
            sync.ProcessError += new UnityCore.CoreHandler(sync_ProcessError);
        }

        void sync_ProcessError(string pcode, string msg)
        {
            t.SafeCall(this, delegate
            {
                MessageBox.Show(msg);
            });
        }

        void sync_ProcessCompleted(string pcode, string msg)
        {
            t.SafeCall(this, delegate
            {
                switch (pcode)
                {
                    case "GetStores":
                        cmbStore.Items.AddRange(sync.stores);
                        break;
                    case "Read":
                        DataTable dt = new DataTable();
                        dt = sync.export;
                        if (dt.Rows.Count > 0)
                        {
                            grd.DataSource = sync.export.DefaultView.ToTable(true, "fAccountName", "fBankName", "fCheckNumber", "fDateReceived", "fAmount", "fDueDate");
                            grd.Columns[0].HeaderText = "Account Name";
                            grd.Columns[1].HeaderText = "Bank Name";
                            grd.Columns[2].HeaderText = "Check Number";
                            grd.Columns[3].HeaderText = "Date Received";
                            grd.Columns[4].HeaderText = "Amount";
                            grd.Columns[5].HeaderText = "Due Date";

                            grd.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                            computeTotalCheque();
                        }
                        else
                        {
                            MessageBox.Show("No Records Found", "Display Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        break;
                }

            });
        }
        void computeTotalCheque()
        {
            double totalC = 0.0;
            for (int i = 0; i < grd.Rows.Count; i++)
            {
                double amtC = double.Parse(grd.Rows[i].Cells[4].Value.ToString());
                totalC += amtC;
            }
            txtTotal.Text = Common.FormatCurrency(totalC);
        }
        void btnView_Click(object sender, EventArgs e)
        {
            if (txtDate.Text != "" && cmbStore.Text != "")
            {
                path.date = txtDate.Text;
                path.remNode = cmbStore.Text;
                path.fileName = "Cheques";
                sync.Read(path);
            }
        }

        
        
        void panel2_EnabledChanged(object sender, EventArgs e)
        {
            if (!panel2.Enabled)
            {
                panel2.BackColor = Color.MidnightBlue;
            }
            else
            {
                panel2.BackColor = Color.RoyalBlue;
            }
        }
        void btnClose_Click(object sender, EventArgs e)
        {
            pnlCheckList.Dispose();
            this.Dispose();
        }
        public Panel PanelForm
        {
            get { return pnlCheckList; }
        }
        public void Initialize(string Title)
        {
            this.Select();
            lblText.Text = Title;
            sync.Stores();
        }

        //printing
        int cnt = 0;
        int pp = 1;
        void btnPrint_Click(object sender, EventArgs e)
        {
            PrintDocument pd = new PrintDocument();
            pd.DocumentName = lblText.Text;
            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
            PrintDialog pdlg = new PrintDialog();
            pdlg.Document = pd;
            if (pdlg.ShowDialog() == DialogResult.OK)
            {
                PrintPreviewDialog ppdlg = new PrintPreviewDialog();
                ppdlg.Document = pd;
                //Tracking.EnterLog("Price Table is printed.", DateTime.Now.ToString());
                ppdlg.ShowDialog();
            }
        }

        void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            float lpp = 0;
            float y = 0;
            float tm = e.MarginBounds.Top;
            float lm = e.MarginBounds.Left - 50;
            float rm = e.PageBounds.Width - 150;
            float bm = e.PageBounds.Height - 100;
            float ht = 0;
            Font cFont;
            StringFormat sf = new StringFormat();


            //Header
            y = 110;
            cFont = new Font("Courier New", 11f, FontStyle.Bold);
            ht = cFont.GetHeight(e.Graphics);
            sf.Alignment = StringAlignment.Center;
            e.Graphics.DrawString(cmbStore.Text, new Font("Courier New", 15f, FontStyle.Bold), Brushes.Black, e.PageBounds.Width/2, 50, sf);
            e.Graphics.DrawString(Common.lStoreAddress, new Font("Courier New", 15f, FontStyle.Bold), Brushes.Black, e.PageBounds.Width / 2, 70, sf);

            sf.Alignment = StringAlignment.Near;
            e.Graphics.DrawString("Check List - Page " + pp, cFont, Brushes.Black, lm, y, sf);
            sf.Alignment = StringAlignment.Far;
            e.Graphics.DrawString(txtDate.Text, cFont, Brushes.Black, rm+100, y, sf);
            //Column Header
            y += ht;

            e.Graphics.FillRectangle(Brushes.LightGray, lm, y, rm - lm + 100, ht);
            e.Graphics.DrawRectangle(new Pen(Brushes.Black), lm, y, rm - lm + 100, ht);
            sf.Alignment = StringAlignment.Center;
            float cx = lm + (((rm - lm) * 30) / 100);
            float tx = lm + (((rm - lm) * 15f) / 100);
            e.Graphics.DrawLine(new Pen(Brushes.Black), cx, y, cx, y + ht);
            e.Graphics.DrawString("Account Name", cFont, Brushes.Black, tx, y, sf);
            
            cx += ((rm - lm) * 25) / 100;
            tx = cx - ((rm - lm) * 25) / 200;
            e.Graphics.DrawLine(new Pen(Brushes.Black), cx, y, cx, y + ht);
            e.Graphics.DrawString("Name of Bank", cFont, Brushes.Black, tx, y, sf);

            cx += ((rm - lm) * 25) / 100;
            tx = cx - ((rm - lm) * 25) / 200;
            e.Graphics.DrawLine(new Pen(Brushes.Black), cx, y, cx, y + ht);
            e.Graphics.DrawString("Check Number", cFont, Brushes.Black, tx, y, sf);

            cx += ((rm - lm) * 18) / 100;
            tx = cx - ((rm - lm) * 18) / 200;
            //e.Graphics.DrawLine(new Pen(Brushes.Black), cx, y, cx, y + ht);
            //e.Graphics.DrawString("Check No: ", cFont, Brushes.Black, tx, y, sf);
            // cx += ((rm - lm) * 18) / 100;
            //tx = cx - ((rm - lm) * 18) / 200;
            e.Graphics.DrawLine(new Pen(Brushes.Black), cx, y, cx, y + ht);
            e.Graphics.DrawString("Due Date", cFont, Brushes.Black, tx, y, sf);
            
            tx += ((rm - lm) * 17) / 100;
            e.Graphics.DrawString("Amount", cFont, Brushes.Black, tx, y, sf);
            //Data
            y += ht * (1.5f);
            cFont = new Font("Courier New", 11f, FontStyle.Regular);
            ht = cFont.GetHeight(e.Graphics);
            lpp = (bm - y) / ht;
            int cl = 0;
            float stx = 0;

            while (cl <= lpp && cnt < grd.Rows.Count)
            {
                sf.Alignment = StringAlignment.Near;
                stx = lm + 1;
                string str = grd.Rows[cnt].Cells[0].Value.ToString();
                if (str.Length > 20)
                {
                    str = str.Substring(0, 17) + "...";
                }
                e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);
                stx += ((rm - lm) * 30) / 100;
                str = grd.Rows[cnt].Cells[1].Value.ToString();
                if (str.Length > 23)
                {
                    str = str.Substring(0, 20) + "...";
                }
                e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);
                stx += ((rm - lm) * 25) / 100;
                
                str = grd.Rows[cnt].Cells[2].Value.ToString();
                e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);
                stx += ((rm - lm) * 25) / 100;

                //str = grd.Rows[cnt].Cells[3].Value.ToString();
                //e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);
                //stx += ((rm - lm) * 20) / 100;
                
                str = grd.Rows[cnt].Cells[5].Value.ToString();
                e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);

                stx += ((rm - lm) * 35) / 100;
                sf.Alignment = StringAlignment.Far;
                str = grd.Rows[cnt].Cells[4].Value.ToString();
                e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);

                y += ht;
                cnt++;
                cl++;
            }
            e.Graphics.DrawString("Total", cFont, Brushes.Blue, stx - 150, y + 10, sf);
            e.Graphics.DrawString(txtTotal.Text, cFont, Brushes.Blue, stx - 4, y+10, sf);

            if (cnt < grd.Rows.Count)
            {
                e.HasMorePages = true;
                pp++;
            }
            else
            {
                e.HasMorePages = false;
                cnt = 1;
                pp = 1;
            }
        }
    }
}
